package com.manager.sensitive.service.impl;

import com.manager.common.dao.SuperDao;
import com.manager.common.utils.ObjectUtils;
import com.manager.common.vo.PageVO;
import com.manager.model.SensitiveWord;
import com.manager.model.SensitiveWordLog;
import com.manager.sensitive.service.SensitiveWordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Component
@Transactional
public class SensitiveWordServiceImpl extends SuperDao<Object> implements SensitiveWordService {

    @Override
    public PageVO getSensitiveWordPage(SensitiveWord model) {
        String sql ="  from SensitiveWord t where 1 = 1 ";
        List<Object> params = new ArrayList<>();
        if(StringUtils.isNotEmpty(model.getWord())){
            sql += " and t.word like # ";
            params.add("%"+model.getWord()+"%");
        }
        sql = ObjectUtils.replaceIndexByList(sql, params);
        List<SensitiveWord> list = this.getList(sql,params.toArray(),model.getStart(),model.getLimit(),"");
        int sum = this.getListCount("select count(*) " + sql, params.toArray(), "");
        int count = (int)Math.ceil(Double.valueOf(sum)/Double.valueOf(model.getLimit()));

        return PageVO.create(model.getStart(),count,list);
    }

    @Override
    public void addSensitiveWord(SensitiveWord model) {
        this.addT(model);
    }

    public void updateSensitiveWord(SensitiveWord model) {
        SensitiveWord sensitiveWord = this.getT(model.getId(),SensitiveWord.class);
        BeanUtils.copyProperties(model,sensitiveWord,new String[]{"id"});
        this.updateT(sensitiveWord);
    }

    @Override
    public void deleteSensitiveWord(SensitiveWord model) {
        SensitiveWord sensitiveWord = this.getT(model.getId(),SensitiveWord.class);
        this.delete(sensitiveWord);
    }

    @Override
    public void updateSensitiveWordEnabled(SensitiveWord model) {
        SensitiveWord sensitiveWord = this.getT(model.getId(),SensitiveWord.class);
        sensitiveWord.setHasEnable(model.getHasEnable());
        this.updateT(sensitiveWord);
    }

    @Override
    public List<SensitiveWord> getSensitiveWordList() {
        String sql = "from SensitiveWord t where t.hasEnable = 1 ";
        List<SensitiveWord> list = this.getList(sql,new Object[]{},0,0,"");
        return list;
    }

    @Override
    public void addHitLog(SensitiveWordLog model) {
        this.addT(model);
    }

    @Override
    public PageVO getHitLogPage(SensitiveWordLog model) {
        String sql ="  from SensitiveWordLog t where 1 = 1 ";
        List<Object> params = new ArrayList<>();
        if(StringUtils.isNotEmpty(model.getWord())){
            sql += " and t.word like # ";
            params.add("%"+model.getWord()+"%");
        }
        sql = ObjectUtils.replaceIndexByList(sql, params);
        List<SensitiveWordLog> list = this.getList(sql,params.toArray(),model.getStart(),model.getLimit(),"");
        int sum = this.getListCount("select count(*) " + sql, params.toArray(), "");
        int count = (int)Math.ceil(Double.valueOf(sum)/Double.valueOf(model.getLimit()));

        return PageVO.create(model.getStart(),count,list);
    }
}

